<?php
/**
 * @user:andy
 * @date:2021/6/16
 * @company:http://www.sjdphp.com
 * @desc:
 */

namespace app\admin\validate;


use think\Validate;

class BackValidate extends Validate
{
    protected $rule = [
        'username'   => 'require',
        'password'   => 'require',
        'captcha'    => 'require|checkCaptcha',
        'back_id'    => 'require|number',
        'back_name'  => 'require',
        'back_phone' => 'require|number|checkPhone',
        'back_email' => 'require|checkEmail',
        'role_id'    => 'require|number',
    ];

    protected $message = [
        'username.require'   => '用户名不能为空',
        'password.require'   => '密码不能为空',
        'captcha.require'    => '验证码不能为空',
        'back_id.require'    => '参数不能为空',
        'back_id.number'     => '参数不合法',
        'back_name.require'  => '管理员名称不能为空',
        'back_phone.require' => '管理员手机号不能为空',
        'back_phone.number'  => '管理员手机号类型错误',
        'back_email.require' => '管理员邮箱不能为空',
        'role_id.require'    => '所属角色不能为空',
        'role_id.number'     => '所属角色类型错误',
    ];

    protected $scene = [
        'sign_in' => ['username','password','captcha'],
        'add'     => ['username','password','back_name','back_phone','back_email','role_id'],
        'edit'    => ['username','password','back_name','back_phone','back_email','role_id','back_id'],
    ];


    protected function checkCaptcha($value){
//        if(!captcha_check($value)){
//            return '验证码输入错误';
//        }
        return true;
    }


    protected function checkPhone($value){
        if(!preg_match("/^((\(\d{2,3}\))|(\d{3}\-))?1(3|5|8|7|9)\d{9}$/",$value)){
            return '输入的手机号有误,请重新输入';
        }
        return true;
    }


    protected function checkEmail($value){
        if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/",$value)){
            return "输入的邮箱有误,请重新输入";
        }
        return true;
    }
}
